﻿
using System;
using System.Collections.Generic;
using System.Text;
using StudentManager.Core;
using StudentManager.Model;
using System.Data.SqlClient;
using System.Data;

namespace StudentManager.DataAccess
{
    class HocSinhAccess
    {
        dbConnection db = new dbConnection();
        public HocSinhAccess()
        {

        }
        //---- Tìm kiếm theo mã học sinh
        public DataView timkiemMahs(string MaGV)
        {
            DataView dataView = layToanbohocsinh().DefaultView;
            dataView.RowFilter = string.Format("MAHS like '%{0}%'", MaGV);
            return dataView;
        }
        //---- Tìm kiếm theo tên học sinh
        public DataView timkiemTenhs(string TenGv)
        {
            DataView dataView = layToanbohocsinh().DefaultView;
            dataView.RowFilter = string.Format("TENHS like '%{0}%'", TenGv);
            return dataView;
        }
        //---- Tim Học sinh theo ma va tra ve mot bang
        public DataTable timHocsinhtheoma(string mahs)
        {
            SqlParameter[] listParam = new SqlParameter[1];
            SqlParameter ma= new SqlParameter("MAHS", SqlDbType.NVarChar, 10);
            ma.Value = mahs;
            listParam[0]= ma;
            return db.executeSelectProcedureQuery("select_hocsinh_theoma", listParam);
        }
        //---- Thêm Học sinh
        public int themHocsinh(HocSinh hs)
        {
            if (timHocsinhtheoma(hs.MAHS1).Rows.Count > 0) return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[5];
                SqlParameter masv = new SqlParameter("@MAHS", SqlDbType.VarChar, 10);
                SqlParameter malh = new SqlParameter("@MALH", SqlDbType.VarChar, 10);
                masv.Value = hs.MAHS1;
                malh.Value = hs.MALH1;
                listParam[0] = masv;
                listParam[1] = new SqlParameter("@TENHS", hs.TENHS1);
                listParam[2] = new SqlParameter("@NGAYSINH", string.Format("{0:yyyy-MM-dd}", hs.NGAYSINH1));
                listParam[3] = new SqlParameter("@GIOITINH", hs.GIOITINH1);
                listParam[4] = malh;
                db.executeInsertProcedureQuery("them_hocsinh", listParam);
                return 1;
            }
        }
        //---- Sửa học sinh
        public int suaHocsinh(HocSinh hs)
        {
            if (timHocsinhtheoma(hs.MAHS1)==null) return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[5];
                SqlParameter masv = new SqlParameter("@MAHS", SqlDbType.VarChar, 10);
                SqlParameter malh = new SqlParameter("@MALH", SqlDbType.VarChar, 10);
                masv.Value = hs.MAHS1;
                malh.Value = hs.MALH1;
                listParam[0] = masv;
                listParam[1] = new SqlParameter("@TENHS", hs.TENHS1);
                listParam[2] = new SqlParameter("@NGAYSINH", string.Format("{0:yyyy-MM-dd}", hs.NGAYSINH1));
                listParam[3] = new SqlParameter("@GIOITINH", hs.GIOITINH1);
                listParam[4] = malh;
                db.executeUpdateProcedureQuery("sua_hocsinh", listParam);
                return 1;
            }
        }
        //----- Xóa học sinh
        public int xoaHocsinh(HocSinh hs)
        {
            if (timHocsinhtheoma(hs.MAHS1) == null) return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[1];
                SqlParameter mahs = new SqlParameter("@MAHS", SqlDbType.VarChar, 10);
                mahs.Value = hs.MAHS1;
                listParam[0] = mahs;
                db.executeDeleteProcedureQuery("xoa_hocsinh", listParam);
                return 1;
            }
        }
        //---- Lấy toàn bộ học sinh
        public DataTable layToanbohocsinh()
        {
            return db.executeSelectProcedureQueryNonParameter("select_hocsinh");
        }
    }
        
}
